﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using efuture.http;
using System.IO;
using efuture.xml;
using efuture.utils;
using DevExpress.XtraTreeList.Nodes;

namespace efuture.vdrp
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm, IPlugin
    {
        private String xx = "";

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnNewFolder_Click(object sender, EventArgs e)
        {
            if (treeList1.FocusedNode == null)
            {
                treeList1.FocusedNode = treeList1.AppendNode(new Object[] { VdrpUtils.getIdentity(), 0, 0, "folder" }, null);
            }
            else
            {
                treeList1.FocusedNode = treeList1.AppendNode(new Object[] { VdrpUtils.getIdentity(), 0, 0, "folder" }, treeList1.FocusedNode);
            }
        }

        private void frmReportList_Load(object sender, EventArgs e)
        {
        }

        private void loadData()
        {
            treeList1.DataSource = null;
            treeList1.Nodes.Clear();

            String xmlData = HttpHelper.DoGet("/report/custom/find", null);
            DataSet ds = new DataSet();
            ds.ReadXmlSchema(Application.StartupPath + "\\Report.xsd");
            ds.ReadXml(new StringReader(xmlData));
            treeList1.DataSource = ds.Tables[0];
            ((DataTable)treeList1.DataSource).DataSet.AcceptChanges();

            if (treeList1.Nodes.Count > 0)
            {
                treeList1.Nodes[0].Expanded = true;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (treeList1.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)treeList1.DataSource;

            StringWriter sw = null;
            if (dt.DataSet.HasChanges(DataRowState.Added))
            {
                DataSet ds2 = dt.DataSet.GetChanges(DataRowState.Added);
                sw = new StringWriter();
                ds2.WriteXml(sw);
                HttpHelper.DoPost("/report/custom/add", sw.ToString(), null);
            }
            if (dt.DataSet.HasChanges(DataRowState.Modified))
            {
                DataSet ds2 = dt.DataSet.GetChanges(DataRowState.Modified);
                sw = new StringWriter();
                ds2.WriteXml(sw);
                HttpHelper.DoPost("/report/custom/update", sw.ToString(), null);
            }

            loadData();
        }

        public void afterPropertiesSet(Dictionary<string, string> properties)
        {
            HttpHelper.serverURL = properties["serverURL"];
        }

        public void shutdown()
        {
            _ctx.OnEvent(EventType.OnBlur);
            _ctx.OnEvent(EventType.OnClosed);
        }

        private IPluginContext _ctx;
        public StartupResult startup(IPluginContext ctx)
        {
            try
            {
                _ctx = ctx;
                _ctx.OnEvent(EventType.OnFocus);
                this.btnSearch_Click(this.btnSearch, EventArgs.Empty);
            }
            catch (Exception e)
            {
                return StartupResult.Failed;
            }
            return StartupResult.Success;
        }

        private void treeList1_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            if ("folder".Equals(e.Node.GetValue(colType)))
            {
                e.Node.StateImageIndex = 0;
            }
            else if ("table".Equals(e.Node.GetValue(colType)))
            {
                e.Node.StateImageIndex = 1;
            }
            else if ("pivot".Equals(e.Node.GetValue(colType)))
            {
                e.Node.StateImageIndex = 2;
            }

        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (treeList1.FocusedNode != null)
            {
                btnEdit.Enabled = !"folder".Equals(treeList1.FocusedNode.GetValue(colType));

                btnNewReport.Enabled = "folder".Equals(treeList1.FocusedNode.GetValue(colType));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (treeList1.FocusedNode == null || treeList1.DataSource == null)
            {
                return;
            }

            foreach (TreeListNode node in treeList1.FocusedNode.Nodes)
            {
                delete(node.GetValue(colId));
            }
            delete(treeList1.FocusedNode.GetValue(colId));


            this.btnSearch_Click(this.btnSearch, EventArgs.Empty);
        }

        private void delete(Object id)
        {
            String xmlData = "<Root><Report><Id>{0}</Id></Report></Root>";
            HttpHelper.DoPost("/report/custom/delete", String.Format(xmlData, id), null);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            shutdown();
        }

        private void btnNewReport_Click(object sender, EventArgs e)
        {
            if (treeList1.DataSource == null || treeList1.FocusedNode == null)
            {
                return;
            }

            frmReportDesign frm = new frmReportDesign();
            frm.ReportId = "0";
            frm.ReportParentId = treeList1.FocusedNode.GetValue(colId).ToString();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                loadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (treeList1.DataSource == null || treeList1.FocusedNode == null)
            {
                return;
            }

            frmReportDesign frm = new frmReportDesign();
            frm.ReportId = treeList1.FocusedNode.GetValue(colId).ToString();
            frm.ReportParentId = treeList1.FocusedNode.GetValue(colParentId).ToString();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                loadData();
            }
        }

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            btnEdit_Click(btnEdit, EventArgs.Empty);
        }

    }
}